package net.lbh.share;

import net.lbh.share.bean.ShareInfo;
import android.app.Activity;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.util.Log;

import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.bean.SocializeConfig;
import com.umeng.socialize.bean.SocializeEntity;
import com.umeng.socialize.controller.UMServiceFactory;
import com.umeng.socialize.controller.UMSocialService;
import com.umeng.socialize.controller.listener.SocializeListeners.SnsPostListener;
import com.umeng.socialize.media.MailShareContent;
import com.umeng.socialize.media.QQShareContent;
import com.umeng.socialize.media.QZoneShareContent;
import com.umeng.socialize.media.SinaShareContent;
import com.umeng.socialize.media.SmsShareContent;
import com.umeng.socialize.media.TencentWbShareContent;
import com.umeng.socialize.sso.EmailHandler;
import com.umeng.socialize.sso.QZoneSsoHandler;
import com.umeng.socialize.sso.RenrenSsoHandler;
import com.umeng.socialize.sso.SinaSsoHandler;
import com.umeng.socialize.sso.SmsHandler;
import com.umeng.socialize.sso.TencentWBSsoHandler;
import com.umeng.socialize.sso.UMQQSsoHandler;
import com.umeng.socialize.sso.UMSsoHandler;
import com.umeng.socialize.weixin.controller.UMWXHandler;
import com.umeng.socialize.weixin.media.CircleShareContent;
import com.umeng.socialize.weixin.media.WeiXinShareContent;
import com.umeng.socialize.yixin.controller.UMYXHandler;

/**
 * 
 * 分享组件 代理
 * 
 * @author BaoHong.Li
 * @date 2015-6-30 下午1:53:06
 * @update (date)
 * @version V1.0
 */
public class ShareAgent {

	private static final String TAG = ShareAgent.class.getName();

	private static final String DESCRIPTOR = "com.umeng.share";

	private static UMSocialService mController = UMServiceFactory
			.getUMSocialService(DESCRIPTOR);

	private static ShareAgent instance;

	private ShareAgent() {
		setDefaultPlatfomrs();
	}

	public static ShareAgent getInstance() {

		synchronized (ShareAgent.class) {

			if (null == instance) {
				instance = new ShareAgent();
			}

		}
		return instance;
	}

	/**
	 * 
	 * 配置 qq开放平台参数
	 * 
	 * @param appId
	 * @param appKey
	 * @return boolean
	 * @autour BaoHong.Li
	 * @date 2015-7-17 下午4:19:22
	 * @update (date)
	 */
	public boolean initQQKeys(String appId, String appKey) {
			return ConstantKeys.initQQKeys(appId, appKey);
	}

	/**
	 * 
	 * 配置 微信开放平台参数
	 * 
	 * @param appId
	 * @param appKey
	 * @return boolean
	 * @autour BaoHong.Li
	 * @date 2015-7-17 下午4:20:21
	 * @update (date)
	 */
	public boolean initWxKeys(String appId, String appSecret) {
		return ConstantKeys.initWxKeys(appId, appSecret);
	}
	
	
	/**
	 *
	 * 配置 人人 开放平台 参数
	* @param appkey
	* @param appSecret
	* @return boolean
	* @autour BaoHong.Li
	* @date 2015-7-23 上午11:50:29 
	* @update (date)
	 */
	public  boolean initRRKeys(String appkey, String appSecret) {
		return ConstantKeys.initRRKeys(appkey, appSecret);
	}

	/**
	 *
	 * 配置 易信 开放平台 参数
	* @param appId
	* @return boolean
	* @autour BaoHong.Li
	* @date 2015-7-23 上午11:53:32 
	* @update (date)
	 */
	public boolean initYxKeys(String appId) {
		return ConstantKeys.initYxKeys(appId);
	}

	
	
	/***
	 * set debug modle
	 * 
	 * @param debug
	 *            [true or false]
	 * @return void
	 * @autour BaoHong.Li
	 * @date 2015年7月22日17:32:06
	 * @update (date)
	 */
	public void setDebug(boolean debug) {
		L.isDebug = debug;
	}

	/**
	 *
	 * 配置默认平台顺序
	* @return void
	* @autour BaoHong.Li
	* @date 2015-8-6 上午9:43:38 
	* @update (date)
	 */
	private void setDefaultPlatfomrs(){
		setPlatforms(SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE,
				SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE, SHARE_MEDIA.SINA,
				SHARE_MEDIA.TENCENT, SHARE_MEDIA.DOUBAN, SHARE_MEDIA.RENREN);
	}
	/**
	 * 
	 * 设置 分享平台
	 * 
	 * @param platforms
	 * @return void
	 * @throws
	 * @autour BaoHong.Li
	 * @date 2015-7-23 上午11:01:04
	 * @update (date)
	 */
	public void setPlatforms(SHARE_MEDIA... platforms) {

		if (mController == null) {
			mController = UMServiceFactory.getUMSocialService(DESCRIPTOR);
		}

		mController.getConfig().setPlatforms(platforms);

		
		// 验证平台参数
		// if (!ConstantKeys.verifyInitKeys(platform)) {
		// throw new IllegalArgumentException("please init platform keys");
		// }
	}
	

	/**
	 * 
	 * 移除分享平台
	 * 
	 * @param platforms
	 * @return void
	 * @throws
	 * @autour BaoHong.Li
	 * @date 2015-7-23 上午11:01:16
	 * @update (date)
	 */
	public void removePlatforms(SHARE_MEDIA... platforms) {
		if (mController == null) {
			mController = UMServiceFactory.getUMSocialService(DESCRIPTOR);
		}

		mController.getConfig().removePlatform(platforms);
	}

	/**
	 * 
	 * 重新 排版 分享平台 面板
	 * 
	 * @param platforms
	 * @return void
	 * @autour BaoHong.Li
	 * @date 2015-7-23 上午11:02:36
	 * @update (date)
	 */
	public void setPlatformOrder(SHARE_MEDIA... platforms) {

		if (mController == null) {
			mController = UMServiceFactory.getUMSocialService(DESCRIPTOR);
		}

		mController.getConfig().setPlatformOrder(platforms);
	}

	public SocializeConfig  getPlatformConfig(){
		return mController.getConfig();
	}
	
	/**
	 * 
	 * 分享到
	 * <p>
	 * default platforms
	 * <p>
	 * <p>
	 * 微信，qq ,qq空间 ， 新浪微博，腾讯微博，人人网
	 * 
	 * @param activity
	 * @param shareInfo
	 * @return void
	 * @autour BaoHong.Li
	 * @date 2015-6-30 下午2:24:37
	 * @update (date)
	 */
	public void share(Activity activity, ShareInfo shareInfo) {
		share(activity, shareInfo,null, null);
	}

	/**
	 * 
	 * 分享到
	 * <p>
	 * default platforms
	 * <p>
	 * <p>
	 * 微信，qq ,qq空间 ， 新浪微博，腾讯微博，人人网
	 * 
	 * @param activity
	 * @param shareInfo
	 * @return void
	 * @autour BaoHong.Li
	 * @date 2015年8月6日09:47:20
	 * @update (date)
	 */
	public void share(Activity activity, ShareInfo shareInfo, OnShareListener listener) {
		share(activity, shareInfo,null, listener);
	}

	/**
	 * 
	 * 分享到
	 * <p>
	 * 定制  platforms
	 * <p>
	 * <p>
	 * 微信，qq ,qq空间 ， 新浪微博，腾讯微博，人人网[ 分享到其中一个]
	 * 
	 * @param activity
	 * @param shareInfo
	 * @return void
	 * @autour BaoHong.Li
	 * @date 2015年8月6日09:47:20
	 * @update (date)
	 */
	public void share(Activity activity, ShareInfo shareInfo,final SHARE_MEDIA platfrom,
			final OnShareListener listener) {

		if (null == shareInfo) {
			new IllegalArgumentException("shareinfo is null");
		}

		if (mController == null) {
			mController = UMServiceFactory.getUMSocialService(DESCRIPTOR);
		}

		configPlatforms(activity);

		initShareInfo(activity, shareInfo);

		if (null != listener) {
			if (null !=platfrom) {
				mController.postShare(activity, platfrom,new SnsPostListener() {
					
					@Override
					public void onStart() {
						if (null !=listener) {
							listener.onStart();
						}
					}
					
					@Override
					public void onComplete(SHARE_MEDIA arg0, int stateCode,
							SocializeEntity arg2) {
						
						switch (stateCode) {
						
						// 分享成功
						case 200:
							if (null !=listener) {
								listener.onSuccess();
							}
							break;
							
							// 分享失败
						default:
							if (null !=listener) {
								listener.onFail();
							}
							break;
						}
					}
				});
				
			}else {
				
				mController.openShare(activity, new SnsPostListener() {
					
					@Override
					public void onStart() {
						if (null !=listener) {
							listener.onStart();
						}
					}
					
					@Override
					public void onComplete(SHARE_MEDIA arg0, int stateCode,
							SocializeEntity arg2) {
						
						switch (stateCode) {
						
						// 分享成功
						case 200:
							if (null !=listener) {
								listener.onSuccess();
							}
							break;
							
							// 分享失败
						default:
							if (null !=listener) {
								listener.onFail();
							}
							break;
						}
					}
				});
				
			}
		} else {
			if (null !=platfrom) {
				mController.postShare(activity, platfrom,new SnsPostListener() {
					
					
					
					@Override
					public void onStart() {
					}
					
					@Override
					public void onComplete(SHARE_MEDIA arg0, int stateCode,
							SocializeEntity arg2) {
						
						switch (stateCode) {
						
						// 分享成功
						case 200:
							L.d(TAG, "share success");
							break;
							
							// 分享失败
						default:
							L.d(TAG, "share fail");
							break;
						}
					}
				});
			}else {
				mController.openShare(activity, false);
			}
		}
	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {

		// 根据requestCode获取对应的SsoHandler
		UMSsoHandler ssoHandler = mController.getConfig().getSsoHandler(
				requestCode);
		if (ssoHandler != null) {
			ssoHandler.authorizeCallBack(requestCode, resultCode, data);
			L.d(TAG, "#### ssoHandler.authorizeCallBack");
		}

	}

	/**
	 * 初始化 分享内容
	 * 
	 * @param activity
	 * @param shareInfo
	 * @return void
	 * @autour BaoHong.Li
	 * @date 2015-6-30 下午2:41:00
	 * @update (date)
	 */
	public static void initShareInfo(Activity activity, ShareInfo shareInfo) {
		// 设置分享内容
		if (TextUtils.isEmpty(shareInfo.getText())) {
			mController.setShareContent(shareInfo.getText());
		}
		if (null != shareInfo.getMusic()) {
			mController.setShareMedia(shareInfo.getMusic());
		}

		mController.setAppWebSite(SHARE_MEDIA.RENREN, shareInfo.getUrl());

		// 微信分享内容
		WeiXinShareContent weixinContent = new WeiXinShareContent();
		weixinContent.setShareContent(shareInfo.getText());
		weixinContent.setTitle(shareInfo.getTitle());
		weixinContent.setTargetUrl(shareInfo.getUrl());
		weixinContent.setShareImage(shareInfo.getImage());
		mController.setShareMedia(weixinContent);

		// 朋友圈分享的内容
		CircleShareContent circleMedia = new CircleShareContent();
		circleMedia.setShareContent(shareInfo.getText());
		circleMedia.setShareImage(shareInfo.getImage());
		circleMedia.setTitle(shareInfo.getTitle());
		circleMedia.setTargetUrl(shareInfo.getUrl());
		mController.setShareMedia(circleMedia);

		// 设置QQ空间分享内容
		QZoneShareContent qzone = new QZoneShareContent();
		qzone.setShareContent(shareInfo.getText());
		qzone.setShareImage(shareInfo.getImage());
		qzone.setTitle(shareInfo.getTitle());
		qzone.setTargetUrl(shareInfo.getUrl());
		mController.setShareMedia(qzone);

		// qq分享内容
		QQShareContent qqShareContent = new QQShareContent();
		qqShareContent.setShareContent(shareInfo.getText());
		qqShareContent.setShareImage(shareInfo.getImage());
		qqShareContent.setTitle(shareInfo.getTitle());
		qqShareContent.setTargetUrl(shareInfo.getUrl());
		mController.setShareMedia(qqShareContent);

		// 邮件
		MailShareContent mail = new MailShareContent();
		mail.setShareContent(shareInfo.getText());
		mail.setShareImage(shareInfo.getImage());
		mail.setTitle(shareInfo.getTitle());
		mController.setShareMedia(mail);

		// 新浪微博
		SinaShareContent sinaContent = new SinaShareContent();
		sinaContent.setTitle(shareInfo.getTitle());
		sinaContent.setShareContent(shareInfo.getText());
		sinaContent.setShareImage(shareInfo.getImage());
		sinaContent.setTargetUrl(shareInfo.getUrl());
		mController.setShareMedia(sinaContent);

		// 腾讯微博
		TencentWbShareContent tencent = new TencentWbShareContent();
		tencent.setTitle(shareInfo.getTitle());
		tencent.setShareContent(shareInfo.getText());
		tencent.setShareImage(shareInfo.getImage());
		tencent.setTargetUrl(shareInfo.getUrl());
		mController.setShareMedia(tencent);

		// 设置短信分享内容
		SmsShareContent sms = new SmsShareContent();
		sms.setShareContent(shareInfo.getText());
		sms.setShareImage(shareInfo.getImage());
		mController.setShareMedia(sms);

		// 关闭地理位置
		mController.getConfig().setDefaultShareLocation(false);
		// 关闭Toast
		mController.getConfig().closeToast();

	}

	/**
	 * 配置分享平台参数</br>
	 */
	private void configPlatforms(Activity activity) {
		// 添加新浪SSO授权
		mController.getConfig().setSsoHandler(new SinaSsoHandler());
		// 添加腾讯微博SSO授权
		mController.getConfig().setSsoHandler(new TencentWBSsoHandler());
		// 添加人人网SSO授权

		RenrenSsoHandler renrenSsoHandler = new RenrenSsoHandler(activity,
				"201874", ConstantKeys.RR_APP_KEY, ConstantKeys.RR_APP_SECRET);
		mController.getConfig().setSsoHandler(renrenSsoHandler);

		// 添加QQ、QZone平台
		addQQQZonePlatform(activity);

		// 添加微信、微信朋友圈平台
		addWXPlatform(activity);
		
//		addYXPlatform(activity);
		
	       // 添加短信平台
        addSMS();
        // 添加email平台
        addEmail();
        
     // 添加来往、来往动态平台
//        addLaiWang();
	}

	/**
	 * @功能描述 : 添加QQ平台支持 QQ分享的内容， 包含四种类型， 即单纯的文字、图片、音乐、视频. 参数说明 : title, summary,
	 *       image url中必须至少设置一个, targetUrl必须设置,网页地址必须以"http://"开头 . title :
	 *       要分享标题 summary : 要分享的文字概述 image url : 图片地址 [以上三个参数至少填写一个] targetUrl
	 *       : 用户点击该分享时跳转到的目标地址 [必填] ( 若不填写则默认设置为友盟主页 )
	 * @return
	 */
	private void addQQQZonePlatform(Activity activity) {
		String appId = ConstantKeys.QQ_APP_ID;
		String appKey = ConstantKeys.QQ_APP_KEY;
		// 添加QQ支持, 并且设置QQ分享内容的target url
		UMQQSsoHandler qqSsoHandler = new UMQQSsoHandler(activity, appId,
				appKey);
//		qqSsoHandler.setTargetUrl("http://www.umeng.com/social");
		qqSsoHandler.addToSocialSDK();

		// 添加QZone平台
		QZoneSsoHandler qZoneSsoHandler = new QZoneSsoHandler(activity, appId,
				appKey);
		qZoneSsoHandler.addToSocialSDK();
	}

	/**
	 * 
	 * addYXPlatform
	 * 
	 * @return void
	 * @autour BaoHong.Li
	 * @date 2015-6-30 下午1:57:40
	 * @update (date)
	 */
	private void addYXPlatform(Activity activity) {

		// 添加易信平台
		UMYXHandler yixinHandler = new UMYXHandler(activity,
				ConstantKeys.YX_APP_ID);
		// 关闭分享时的等待Dialog
		yixinHandler.enableLoadingDialog(false);
		// 设置target Url, 必须以http或者https开头
//		yixinHandler.setTargetUrl("http://www.umeng.com/social");
		yixinHandler.addToSocialSDK();

		// 易信朋友圈平台
		UMYXHandler yxCircleHandler = new UMYXHandler(activity,
				ConstantKeys.YX_APP_ID);
		yxCircleHandler.setToCircle(true);
		yxCircleHandler.addToSocialSDK();

	}

	/**
	 * 
	 * 添加微信平台分享
	 * 
	 * @return void
	 * @autour BaoHong.Li
	 * @date 2015-6-30 下午2:13:06
	 * @update (date)
	 */
	private void addWXPlatform(Activity activity) {
		// 注意：在微信授权的时候，必须传递appSecret
		// wx967daebe835fbeac是你在微信开发平台注册应用的AppID, 这里需要替换成你注册的AppID
		String appId = ConstantKeys.WX_APP_ID;
		String appSecret = ConstantKeys.WX_APP_SECRET;
		// 添加微信平台
		UMWXHandler wxHandler = new UMWXHandler(activity, appId, appSecret);
		wxHandler.addToSocialSDK();

		// 支持微信朋友圈
		UMWXHandler wxCircleHandler = new UMWXHandler(activity, appId,
				appSecret);
		wxCircleHandler.setToCircle(true);
		wxCircleHandler.addToSocialSDK();
	}
	
    /**
     * * 添加短信平台</br>
    * @return void
    * @autour BaoHong.Li
    * @date 2015-8-6 上午11:48:02 
    * @update (date)
     */
    private void addSMS() {
        // 添加短信
        SmsHandler smsHandler = new SmsHandler();
        smsHandler.addToSocialSDK();
    }
    
    /**
     *
     * 添加Email平台</br>
    * @return void
    * @autour BaoHong.Li
    * @date 2015-8-6 上午11:48:19 
    * @update (date)
     */
    private void addEmail() {
        // 添加email
        EmailHandler emailHandler = new EmailHandler();
        emailHandler.addToSocialSDK();
    }
    
    /**
     *  添加来往和来往动态平台</br>
    * @return void
    * @autour BaoHong.Li
    * @date 2015-8-6 上午11:49:40 
    * @update (date)
     */
//    private void addLaiWang(Activity activity) {
//
//        String appToken = "laiwangd497e70d4";
//        String secretID = "d497e70d4c3e4efeab1381476bac4c5e";
//        // laiwangd497e70d4:来往appToken,d497e70d4c3e4efeab1381476bac4c5e:来往secretID
//        // 添加来往的支持
//        UMLWHandler umlwHandler = new UMLWHandler(activity, appToken, secretID);
//        umlwHandler.addToSocialSDK();
//
//        // 添加来往动态的支持
//        UMLWHandler lwDynamicHandler = new UMLWHandler(getActivity(),
//                appToken, secretID);
//        lwDynamicHandler.setToCircle(true);
//        lwDynamicHandler.addToSocialSDK();
//    }

}
